//
//  GJFInspirationViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/20.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFInspirationViewController.h"
#import "GJFInspirationModel.h"
#import "GJFInspirationCollectionViewCell.h"
#import "GJFInspirationDetailViewController.h"

@interface GJFInspirationViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrModel;
@property (nonatomic, assign) NSInteger page_index;
@end

@implementation GJFInspirationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page_index = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getInspirationData];
}

- (void)getInspirationData {
    NSDictionary *dict = @{@"page_index":@1};
    [GJFNetworkTool POSTRequestWithUrl:APP_INSPIRATIONLIST params:dict complete:^(NSDictionary *result) {
        [self.collectionView.mj_header endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.arrModel = [GJFInspirationModel transformWithArr:array];
            [self.collectionView reloadData];
        }
    }];
}
- (void)getInspirationMoreData {
    NSDictionary *dict = @{@"page_index":@(++_page_index)};
    [GJFNetworkTool POSTRequestWithUrl:APP_INSPIRATIONLIST params:dict complete:^(NSDictionary *result) {
        [self.collectionView.mj_footer endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            [self.arrModel addObjectsFromArray:[GJFInspirationModel transformWithArr:array]];
            [self.collectionView reloadData];
        }
        else {
            [self.view showWarning:result[@"msg"]];
            self.page_index--;
        }
    }];
}
#pragma mark - CollectionView分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
#pragma mark - CollectionView每个分区中item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrModel.count;
}
#pragma mark - CollectionView每个分区中item的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFInspirationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InspirationCell" forIndexPath:indexPath];
    cell.inspirationModel = self.arrModel[indexPath.row];
    cell.imageV.contentMode = UIViewContentModeScaleAspectFill;
    cell.imageV.clipsToBounds = YES;
    return cell;
}
#pragma mark - 点击跳转方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFInspirationDetailViewController *detailVC = [[GJFInspirationDetailViewController alloc] init];
    detailVC.inspirationArr = self.arrModel;
    detailVC.indexP = indexPath;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((WIDTH - 5) / 2, (WIDTH - 5) / 2);
        layout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor colorWithRed:0.91 green:0.90 blue:0.90 alpha:1.00];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[GJFInspirationCollectionViewCell class] forCellWithReuseIdentifier:@"InspirationCell"];
    
        [self.view addSubview:_collectionView];
        MJRefreshGifHeader *header = [GJFRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getInspirationData)];
        _collectionView.mj_header = header;
        
        _collectionView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(getInspirationMoreData)];
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
